package com.honghu.villagepost.activity.fragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.honghu.villagepost.R;
import com.honghu.villagepost.adapter.PagerAdapter;
import com.honghu.villagepost.base.BaseFragment;
import com.honghu.villagepost.constant.IntentKeyConstant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DemandSubTabFragment extends BaseFragment {
    @BindView(R.id.tabs)
    SlidingTabLayout mTabs;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    private ArrayList<Fragment> mFragments = new ArrayList<>();

    public static DemandSubTabFragment newInstance(String type) {
        DemandSubTabFragment fragment = new DemandSubTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString(IntentKeyConstant.CATE_ID, type);
        fragment.setArguments(bundle);
        return fragment;

    }


    @Override
    protected View initViews(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_demand_sub_tab, container, false);
        return view;
    }

    @Override
    protected void initData() {

        mFragments.add(DemandListFragment.newInstance(""));
        mFragments.add(DemandListFragment.newInstance(""));
        mFragments.add(DemandListFragment.newInstance(""));
        mFragments.add(DemandListFragment.newInstance(""));


        List<String> titles = new ArrayList<>();
        titles.add("全部");
        titles.add("大豆");
        titles.add("玉米");
        titles.add("小麦");

        mViewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), mFragments, titles));

        mTabs.setViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(titles.size() - 1);
        mTabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabs.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

}
